<?php
namespace app\restaurant\controller;

use app\ApiRest;
use app\dynamic\model\CardUser;
use app\dynamic\model\UserInfo;
use app\restaurant\model\SpePrice;
use app\restaurant\model\StoreStaff;
use think\App;

use app\restaurant\model\Car as model;

use app\restaurant\model\Order;

use app\restaurant\model\Goods;

use app\restaurant\model\FeedingCar;

use think\facade\Db;


class IndexCar extends ApiRest
{

    protected $model;

    protected $staff_model;

    protected $order_model;

    protected $goods_model;

    protected $feeding_car_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->staff_model = new StoreStaff();

        $this->order_model = new Order();

        $this->goods_model = new Goods();

        $this->feeding_car_model = new FeedingCar();

        if(empty($this->getUserInfo()['store_id'])){

            $this->errorMsg('请选择门店');
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-16 11:24
     * @功能说明:购物车列表
     */
    public function carList(){

        $input = $this->_input;

        $data = $this->model->carList($input['type'],$this->getUserId(),$this->_uniacid,$this->getUserInfo()['store_id']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-16 14:16
     * @功能说明:加入购物车
     */
    public function addCar(){

        $input = $this->_input;

        if(!empty($input['feeding'])){

            sort($input['feeding']);
        }

        $goods = $this->goods_model->goodsInfo(['id'=>$input['goods_id'],'status'=>1]);

        if(empty($goods)){

            $this->errorMsg('该商品已经下架');
        }

        $res = $this->goods_model->checkGoods($this->getUserId(),$input['goods_id'],1);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);

        }
        //添加
        $dis = [

            'user_id' => $this->getUserId(),

            'store_id'=> $this->getUserInfo()['store_id'],

            'goods_id'=> $input['goods_id'],

            'status'  => 1,

            'norml'   => $input['spe_price_id'],

            'uniacid' => $this->_uniacid,

            'feeding' => !empty($input['feeding'])?implode(',',$input['feeding']):''
        ];

        $spe_price_model = new SpePrice();

        $spe_price = $spe_price_model->where(['id'=>$input['spe_price_id'],'goods_id'=>$input['goods_id']])->find();

        if(empty($spe_price)){

            $this->errorMsg('该规格已下架');
        }

        $have_num = $this->model->where(['user_id'=>$this->getUserId(),'store_id'=> $this->getUserInfo()['store_id'],'goods_id'=>$input['goods_id']])->sum('num');

        $num      = $goods['buy_start'] - $have_num>1?$goods['buy_start'] - $have_num:1;

        $data = $this->model->carInfo($dis);

        if(!empty($data)){

            $res = $this->model->carUpdate($dis,['num'=>$data['num']+$num]);

        }else{
            //数量
            $dis['num'] = $num;

            $res = $this->model->carAdd($dis);

        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-16 14:41
     * @功能说明:修改购物车数量
     */
    public function updateCar(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];
        //购物车
        $car = $this->model->carInfo($dis);
        //商品
        $goods = $this->goods_model->goodsInfo(['id'=>$car['goods_id'],'status'=>1]);

        if(empty($goods)){

            $this->errorMsg('该商品已经下架');
        }

        $have_num = $this->model->where(['user_id'=>$this->getUserId(),'store_id'=> $this->getUserInfo()['store_id'],'goods_id'=>$car['goods_id']])->sum('num');

        if($input['type']==1){

            $num      = $goods['buy_start'] - $have_num>1?$goods['buy_start'] - $have_num:1;

            $res = $this->goods_model->checkGoods($this->getUserId(),$car['goods_id'],$num);

            if(!empty($res['code'])){

                $this->errorMsg($res['msg']);

            }

            $res = $this->model->where($dis)->update(['num'=>Db::raw("num+$num")]);

        }else{

            $res = $this->model->where($dis)->update(['num'=>Db::raw("num-1")]);

            $have_num = $this->model->where(['user_id'=>$this->getUserId(),'store_id'=> $this->getUserInfo()['store_id'],'goods_id'=>$car['goods_id']])->sum('num');

            if($have_num<$goods['buy_start']){

                $this->model->where(['user_id'=>$this->getUserId(),'store_id'=> $this->getUserInfo()['store_id'],'goods_id'=>$car['goods_id']])->delete();

            }
            $car = $this->model->carInfo($dis);
            //删除
            if(!empty($car)&&$car['num']<=0){

                $res = $this->model->where($dis)->delete();

            }
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-16 14:48
     * @功能说明:删除购物车
     */
    public function delCar(){

        $input = $this->_input;

        $dis = [

            'user_id' => $this->getUserId(),

            'store_id'=> $this->getUserInfo()['store_id']
        ];

        $res = $this->model->where($dis)->delete();

        return $this->success($res);

    }









}
